<?php
namespace Home\Controller;
use Think\Controller;

class LoginController extends Controller{

    public function index() {
    	$this->display();
    }
    
    public function logincheck(){
    	 if(IS_POST){
            // 做一个简单的登录 组合where数组条件 
            $map=I('post.');
            $map['password']=md5($map['password']);
            //dump($map);die;
            $data=M('Users')->where($map)->find();
            //dump($data);
            if (empty($data)) {
                $this->error('账号或密码错误');
            }else{
                $_SESSION['user']=array(
                    'id'=>$data['id'],
                    'username'=>$data['username'],
                    'realname'=>$data['realname'],
                    'avatar'=>$data['avatar']
                    );
               // $this->success('登录成功、前往管理后台',U('Admin/Index/index'));
               $this->success('登录成功，前往管理页面',U('Depart/Index/index'));
            }
        }else{
            $this->error('登录失败',U('Home/Login/index'));
        }
    }
    public function logout(){
        session('user',null);
        $this->success('退出成功、前往登录页面',U('Home/Login/index'));
    }
    	/**
	 * 修改密码
	 */
	public function changepwd(){
		$user = session('user');
		//dump($user);die;
		if(IS_POST){
			$pwd=I('post.');
			
			if($pwd['newpwd']==$pwd['newpwd1']){
				$map['id']=$user['id'];
				$map['password']=md5($pwd['oldpwd']);
				
				$data=M('Users')->where($map)->select();
				if(empty($data)){
					$this->error('密码不一致，修改失败',U('Home/Login/changepwd'));
				}else{
					
					$password['password']=md5($pwd['newpwd']);
					$result=M('Users')->where($map)->save($password);
					$this->success('修改成功',U('Home/Index/first'));
				}
			}else {
				$this->error('密码不一致，修改失败',U('Home/Login/changepwd'));
			}
			
		}else{
			$this->display();
		}
	}
    
}
?>